package com.imay.dialog;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.imay.capturefilter.utils.ICUtils;
import com.yalantis.ucrop.UCrop;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static com.imay.dialog.Dialoger.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE;
import static com.imay.dialog.Dialoger.SYS_IMAGE_ACTIVITY_REQUEST_CODE;
import static com.imay.dialog.Dialoger.fileUri;

/**
 * Created by Administrator on 2017/3/27.
 */

public class LoadActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Dialoger.showAcionSheetDialog(this);
    }

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        switch (requestCode) {
            case SYS_IMAGE_ACTIVITY_REQUEST_CODE:
                if (resultCode == RESULT_OK) {
                    Uri uri = data.getData();
                    String path = Dialoger.getPath(LoadActivity.this, uri);
                    File file = ICUtils.getOutputMediaFile(LoadActivity.this);
                    UCrop.Options options = new UCrop.Options();
                    options.setToolbarColor(Color.BLACK);
                    options.setActiveWidgetColor(Color.BLACK);
                    options.setStatusBarColor(Color.BLACK);
                    options.setHideBottomControls(true);
                    UCrop.of(Uri.fromFile(new File(path)), Uri.fromFile(file))
                            .withOptions(options)
                            .withAspectRatio(9, 9)
                            .withMaxResultSize(300, 300)
                            .start(LoadActivity.this);
                } else {
                    finish();
                }
                break;
            case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE:
                if (RESULT_OK == resultCode) {
                    if (data != null) {
                        Toast.makeText(this, "Image saved to:\n" + data.getData(),
                                Toast.LENGTH_LONG).show();
                        if (data.hasExtra("data")) {
                            FileOutputStream b = null;
                            try {
                                File file = ICUtils.getFileDirImg(LoadActivity.this);
                                Bitmap thumbnail = data.getParcelableExtra("data");
                                b = LoadActivity.this.openFileOutput(file.getName(), MODE_APPEND);
                                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
                                fileUri = Uri.fromFile(file);
                                File file2 = ICUtils.getFileDirImg(LoadActivity.this);
                                UCrop.Options options = new UCrop.Options();
                                options.setToolbarColor(Color.BLACK);
                                options.setActiveWidgetColor(Color.BLACK);
                                options.setStatusBarColor(Color.BLACK);
                                options.setHideBottomControls(true);
                                UCrop.of(fileUri, Uri.fromFile(file2))
                                        .withOptions(options)
                                        .withAspectRatio(9, 9)
                                        .withMaxResultSize(300, 300)
                                        .start(LoadActivity.this);
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                try {
                                    b.flush();
                                    b.close();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    } else {
                        File file = null;
                        try {
                            file = ICUtils.getFileDirImg(LoadActivity.this);
                            UCrop.Options options = new UCrop.Options();
                            options.setToolbarColor(Color.BLACK);
                            options.setActiveWidgetColor(Color.BLACK);
                            options.setStatusBarColor(Color.BLACK);
                            options.setHideBottomControls(true);
                            UCrop.of(fileUri, Uri.fromFile(file))
                                    .withOptions(options)
                                    .withAspectRatio(9, 9)
                                    .withMaxResultSize(300, 300)
                                    .start(LoadActivity.this);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    finish();
                }
                break;
            case UCrop.REQUEST_CROP:
                final Uri resultUri = UCrop.getOutput(data);
                if (resultUri != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("filePath", Dialoger.getPath(LoadActivity.this, resultUri));
                    Intent intent = getIntent().putExtras(bundle);
                    setResult(RESULT_OK, intent);
                }
                finish();
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;

        }
    }
}
